package com.olinsports.system.olinsportssystem.service;

import com.olinsports.system.olinsportssystem.model.CommodityDetailed;
import com.olinsports.system.olinsportssystem.model.CommodityType;
import com.olinsports.system.olinsportssystem.model.InventoryDetails;
import com.olinsports.system.olinsportssystem.model.MultiCondition;
import com.olinsports.system.olinsportssystem.repository.CommodityDetailedRepository;
import com.olinsports.system.olinsportssystem.repository.CommodityTypeRepository;
import com.olinsports.system.olinsportssystem.util.PageUtil;
import com.sun.javafx.scene.control.skin.LabeledImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/22.
 */
@Service
public class CommodityDetailedService {
    @Autowired
    private CommodityDetailedRepository commodityDetailedRepository;
    @Autowired
    private CommodityTypeService commodityTypeService;
    private int totalPages = 0;//总页数

    private int totalRows;//总数据量
    @Autowired
    private PageUtil pageUtil;
    @Autowired
    private HttpServletRequest request;


    public List<CommodityDetailed> selectAll() {
        return commodityDetailedRepository.selectAll();
    }

    public int insertAll(CommodityDetailed commodityDetailed) {
        return commodityDetailedRepository.insertAll(commodityDetailed);
    }

    public int deleteByKey(int detailedKey) {
        return commodityDetailedRepository.deleteByKey(detailedKey);
    }

    public int updateData(CommodityDetailed commodityDetailed) {
        return commodityDetailedRepository.updateData(commodityDetailed);
    }

    public CommodityDetailed selectByKey(int detailedKey) {
        return commodityDetailedRepository.selectByKey(detailedKey);
    }

    public int saveOrUpdateCommDetal(CommodityDetailed commodityDetailed, int typeKey) {

        CommodityDetailed selectCommDetal = commodityDetailedRepository.selectByKey(commodityDetailed.getDetailedKey());

        CommodityType commodityType = commodityTypeService.selectByTypeKey(typeKey);

        Date date = new Date(System.currentTimeMillis());//获取当前时间

        if (selectCommDetal == null) {//添加操作
            commodityDetailed.setSkuType(commodityType);
            commodityDetailed.setAmdLog(date);
            return commodityDetailedRepository.insertAll(commodityDetailed);
        } else {//更新操作
            commodityDetailed.setSkuType(commodityType);
            return commodityDetailedRepository.updateData(commodityDetailed);
        }
    }

    public Map listDetailed(String page, String rows, boolean _search, String sord, String sidx) {
        int beg = (Integer.valueOf(page) - 1) * Integer.valueOf(rows);
        if (_search) {//进行查找操作
            List<MultiCondition> multiList = pageUtil.searchList(page, rows, _search, request);

            //通过条件进行查询总数据量
            totalRows = commodityDetailedRepository.findByParamsCount(multiList);

            System.out.println("totalRows = " + totalRows);
            //通过条件查询beg-rows的数据，分页查询
            List<CommodityDetailed> stmCountSearch = commodityDetailedRepository.findByParamsLimit(sord, sidx, multiList, beg, Integer.valueOf(rows));
            Map mapSearch = pageUtil.paging(rows, page, totalRows);
            //设置表格数据
            mapSearch.put("rows", stmCountSearch);
            return mapSearch;
        } else {
            //查询当前表中的总数据量
            totalRows = commodityDetailedRepository.findCount();
            System.out.println("totalRows = " + totalRows);
            //通过分页进行查询当前表中的数据
            List<CommodityDetailed> stmCountList = commodityDetailedRepository.findLimit(sord, sidx, beg, Integer.valueOf(rows));

            Map mapList = pageUtil.paging(rows, page, totalRows);
            mapList.put("rows", stmCountList);//设置行显示的数据

            return mapList;
        }

    }

    public Map searchSelf(String page, String rows,String field,String fieldValue){
        int beg = (Integer.valueOf(page)-1)*Integer.valueOf(rows);
        totalRows = commodityDetailedRepository.selectSelfCount(field,fieldValue);
        List<CommodityDetailed> stmCountList =  commodityDetailedRepository.selectSelfLimit(field,fieldValue,beg,Integer.valueOf(rows));
        Map mapList = pageUtil.paging(rows,page,totalRows);
        mapList.put("rows",stmCountList);//设置行显示的数据
        return mapList;
    }

    public List<CommodityDetailed> selectBySkuType(int skuType){
        return commodityDetailedRepository.selectBySkuType(skuType);
    }


}
